package fr.afcepf.atod.projet.facades;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import fr.afcepf.atod.projet.services.interfaces.IServiceCours;
import fr.afcepf.atod.projet.services.interfaces.IServicePersonne;

public class Facade implements IFacade {

	private static Facade uniqueFacade = null;
	private IServicePersonne servicePersonne;
	private IServiceCours serviceCours;
	
	
	public static IFacade getInstance(){
		if(uniqueFacade==null){
			uniqueFacade=new Facade();
		}
		return uniqueFacade;
	}
	
	public static IFacade getInstance(ServletContext servletContext) {
		if(uniqueFacade == null) {
			uniqueFacade = new Facade(servletContext);
		}
		return uniqueFacade;
	}
	
	private Facade(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("springConfig.xml");
		this.setServicePersonne((IServicePersonne) ctx.getBean(IServicePersonne.class)); // selon type
		this.setServiceCours((IServiceCours) ctx.getBean(IServiceCours.class));
	}
	
	private Facade(ServletContext servletContext){
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		this.setServicePersonne((IServicePersonne) ctx.getBean(IServicePersonne.class)); // selon type
		this.setServiceCours((IServiceCours) ctx.getBean(IServiceCours.class));
	}
	
	@Override
	public IServiceCours getServiceCours() {
		return this.serviceCours;
	}

	@Override
	public IServicePersonne getServicePersonne() {
		return this.servicePersonne;
	}

	public final void setServicePersonne(IServicePersonne servicePersonne) {
		this.servicePersonne = servicePersonne;
	}

	public final void setServiceCours(IServiceCours serviceCours) {
		this.serviceCours = serviceCours;
	}

	
}
